package com.gxwgy.wg2404.controller;

import com.gxwgy.wg2404.pojo.EventsPojo;
import com.gxwgy.wg2404.util.DBUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 姚相国
 * @version 1.0
 * @className OrganizerDashboardServlet
 * @date 2024/12/18 02:25
 */
@WebServlet({"/organizer/dashboard"})
public class OrganizerDashboardServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String role = (String) session.getAttribute("role");

        if ("organizer".equals(role)) {
            // 定义一个集合来存放所有的信息
            List<EventsPojo> eventsPojos = new ArrayList<>();

            // 连接数据库，查询所有
            Connection conn = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
            try {
                conn = DBUtil.getConnection();
                // 执行查询语句
                String sql = "select id, name, description, start_time, end_time, location, capacity, current_enrollment, status, created_at, updated_at from events";
                ps = conn.prepareStatement(sql);
                rs = ps.executeQuery(sql);
                // 遍历结果集
                while (rs.next()) {
                    String poll_id = rs.getString("id");
                    String name = rs.getString("name");
                    String description = rs.getString("description");
                    String start_time = rs.getString("start_time");
                    String end_time = rs.getString("end_time");
                    String location = rs.getString("location");
                    int capacity = rs.getInt("capacity");
                    int current_enrollment = rs.getInt("current_enrollment");
                    String status = rs.getString("status");
                    String created_at = rs.getString("created_at");
                    String updated_at = rs.getString("updated_at");

                    // 以上零散的数据封装成java对象
                    EventsPojo eventsPojo = new EventsPojo();
                    eventsPojo.setId(poll_id);
                    eventsPojo.setName(name);
                    eventsPojo.setDescription(description);
                    eventsPojo.setStart_time(start_time);
                    eventsPojo.setEnd_time(end_time);
                    eventsPojo.setLocation(location);
                    eventsPojo.setCapacity(capacity);
                    eventsPojo.setCurrent_enrollment(current_enrollment);
                    eventsPojo.setStatus(status);
                    eventsPojo.setCreated_at(created_at);
                    eventsPojo.setUpdated_at(updated_at);

                    // 收集所有信息
                    eventsPojos.add(eventsPojo);
                }

            } catch (SQLException e) {
                e.printStackTrace();
                throw new ServletException("数据库查询失败", e);
            }finally {
                // 关闭数据库连接
                DBUtil.close(conn, ps, rs);
            }
            //将list集合放到请求域当中
            request.setAttribute("events", eventsPojos);
            // 显示主办方页面内容
            request.getRequestDispatcher("/manageEvents.jsp").forward(request, response);
        } else {
            // 普通用户或管理员没有权限访问
            response.sendRedirect(request.getContextPath() + "/error.jsp");
        }
    }
}

